const app = getApp()
var startY = 0,
  moveY = 0,
  pageAtTop = true;
Component({
  data: {
    isAdmin: false,
    avatarUrl: "/images/share1.png",
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    Custom: app.globalData.Custom,
    coverTransform: "translateY(0px)",
    coverTransition: "0s",
    moving: !1
  },
  methods: {
    onLoad() {
      if (!wx.getStorageSync('openid')) {
        this.goLogin()
      }
      wx.cloud.callFunction({
          name: 'adminAuth'
        })
        .then(res => {
          console.log('adminAuth', res.result)
          if (res.result) {
            this.setData({
              isAdmin: true,
            })
          }
        })
    },
    onShow() {
      this.setData({
        openid: wx.getStorageSync('openid'),
        userInfo: wx.getStorageSync('userInfo'),
        userRole: wx.getStorageSync('userRole')
      })
      if (wx.getStorageSync('openid')) {
        this.user_list()
      }
    },
    async user_list() {
      var songNum
      var discussNum
      var signNum
      var role
      var score
      await wx.cloud.database().collection('song')
        .where({
          _openid: this.data.openid
        })
        .count().then(res => {
          songNum = res.total
        })
      await wx.cloud.database().collection('discuss-post')
        .where({
          _openid: this.data.openid
        })
        .count().then(res => {
          discussNum = res.total
        })
      await wx.cloud.database().collection('user')
        .doc(this.data.openid)
        .field({
          sign: true,
          score: true
        })
        .get()
        .then(res => {
          signNum = res.data.sign,
            score = res.data.score
        })
      await this.setData({
        songNum: songNum,
        discussNum: discussNum,
        signNum: signNum,
        score: score
      })
    },
    authorization() {
      wx.requestSubscribeMessage({
        tmplIds: ['TyKWm6BCbHIDLGItErh7k56f6IhDYNsM7z5uVQithI4',
          'GSNm6M5LHxPYrlXm5k2cCb2Md-yI9zYAQ0n-BRKdPP0'
        ],
        success(res) {
          console.log('已授权接收订阅消息', res)
        },
        fail(err) {
          console.log(err)
        }
      })
    },
    jump(e) {
      if (wx.getStorageSync('openid')) {
        wx.navigateTo({
          url: "/pages/" + e.currentTarget.dataset.a + "?id=" + e.currentTarget.dataset.id
        })
      } else {
        this.goLogin()
      }
    },
    goLogin() {
      wx.navigateTo({
        url: '/pages/user/login/login',
      })
    },
    coverTouchstart(e) {
      if (this.data.pageAtTop === false) {
        return;
      }
      this.setData({
        coverTransition: 'transform .1s linear',
        startY: e.touches[0].clientY
      })
    },
    coverTouchmove(e) {
      moveY = e.touches[0].clientY;
      var moveDistance = moveY - this.data.startY;
      if (moveDistance < 0) {
        // this.data.moving = false;
        this.data.moving = true;
        if (375 + moveDistance > this.data.CustomBar + this.data.StatusBar + 140) {
          this.setData({
            heights: moveDistance
          })
        }
      } else {
        this.data.moving = true;
        if (moveDistance >= 80 && moveDistance < 100) {
          moveDistance = 80;
          this.setData({
            coverTransform: "translateY(".concat(moveDistance, "px)")
          })
        }
        if (moveDistance > 0 && moveDistance <= 80) {
          this.setData({
            coverTransform: "translateY(".concat(moveDistance, "px)")
          })
        }
      }

    },
    coverTouchend() {
      if (this.data.moving === false) {
        return;
      }
      console.log(this.data.heights)
      if (this.data.heights < -140) {
        this.setData({
          heights: 0
        })
      }
      this.setData({
        moving: false,
        coverTransition: 'transform 0.3s cubic-bezier(.21,1.93,.53,.64)',
        coverTransform: 'translateY(0px)',
      })
    },
    onShareAppMessage: function (res) {
      return {
        title: 'TLradio',
        path: '/pages/index/index',
      }
    }
  }
})